package com.itheima.d1_ThreadPool_Runnable;

import java.util.concurrent.*;

/**
 * 1.学会线程池
 *
 public ThreadPoolExecutor(int corePoolSize,
 int maximumPoolSize,
 long keepAliveTime,
 TimeUnit unit,
 BlockingQueue<Runnable> workQueue,
 ThreadFactory threadFactory,
 RejectedExecutionHandler handler)
 */
public class ExecutorServiceDemo {
    public static void main(String[] args) {
        ExecutorService pool = new ThreadPoolExecutor(3,5,5, TimeUnit.SECONDS
                ,new ArrayBlockingQueue<>(5)
                , Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
       // 2.给任务处理
        Runnable runnable = new MyRunnable();
        // 3. 把任务扔给线程池，怎么仍
        pool.execute(runnable);
        pool.execute(runnable);
        pool.execute(runnable);
        //下面这个会处理么？ 我现在线程池里有3个核心线程，我有6个任务
        //下面这五个会进入消息队列，消息队列还没有满，所以不会创建临时线程
        pool.execute(runnable);
        pool.execute(runnable);
        pool.execute(runnable);
        pool.execute(runnable);
        pool.execute(runnable);
        // 这个时候我再加任务,这个时候会创建临时线程，但会创建多少个 1个还是2个不清楚，最多不超过两个
        pool.execute(runnable);
        pool.execute(runnable);
        //触发拒绝策略
       // pool.execute(runnable);
        // 关闭线程池：开发一般不会使 ; 会丢失任务，我们就是需要长期使用  pool.shutdownNow();/ pool.shutdown()等待当前任务完成后再关


    }
}
